var RenderUrlsToFile, arrayOfUrls, system;
			
		
			system = require("system");
		
			/*
			Render given urls
			@param array of URLs to render
			@param callbackPerUrl Function called after finishing each URL, including the last URL
			@param callbackFinal Function called after finishing everything
			*/
		
		
			RenderUrlsToFile = function(urls, callbackPerUrl, callbackFinal) {
			    var getFilename, next, page, retrieve, urlIndex, webpage;
			    urlIndex = 0;
			    webpage = require("webpage");
			    page = null;
				var file="";
			
			    next = function(status, url, file) {
			        page.close();
			        callbackPerUrl(status, url, file);
			        return retrieve();
			    };
					
			    retrieve = function() {
			        var url;
			        if (urls.length > 0) {
			            url = urls.shift();
						var linkData = url;
						url = url.link;
		
			            urlIndex++;
			            page = webpage.create();
			    
						//Them vao ---------------------------
						page.settings.loadImages=false;
						page.onResourceRequested = function(request, net) {
							//console.log(request.url);
						  	if (request.url.indexOf('http://tunghoanh.com/') === 0 && request.url.indexOf('http://ajax.googleapis.com/') === 0 && request.url.indexOf('http://mong.vn/') === 0){
								net.abort();
							}else{
								//console.log(request.url);
							}
						}
		
						page.onConsoleMessage = function(msg) {
						    console.log(msg);
						};
		
						page.onLoadFinished = function() {
							page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", function() {
								page.evaluate(function(linkData) {
			
									if($("#chapter_content").html().length){
										trangthai = 1;
									}else{
										trangthai = 0;
									}
					
									if($(".ctitle").find("img").length == 0){
										linkData.is_image = true;
										linkData.image_get = 0;
									}else{
										linkData.is_image = false;
										linkData.image_get = 1;
									}
			    					$.ajax({
			    						url : 'http://mong.vn/abc.php?putChapterContent=1&sourceAlias=tunghoanh',
			    						type: 'POST',
			    						async: false,
			    						cache: false,
			    						data: {
						
											tap: linkData.tap,
											truyen_id: linkData.story_id,
											is_image: linkData.is_image,
											image_get: linkData.image_get,
											chapt: linkData.thutu,
											title: $(".ctitle").html(),
											content: $("#chapter_content").html(),
											truyen_chuong_link_id: linkData.id,
											trangthai: trangthai,
										},
		
			    						success: function(data){
			    							console.log(data);
			    						},
		
										error: function(jqxhr,textStatus,errorThrown){
				                       		console.log(jqxhr);
				                        	console.log(textStatus);
				                        	console.log(errorThrown);
				                        } //End of log error
		
			    					});// End ajax
		    					}, linkData); //End of try evaluation
	    						return next(status, url, file);
							}); //end page.include js
						}
		
			            page.open(url, function(status) {
			            	if(status === "success") {
			            		// page.onLoadFinished = function(status) {
								// }; // end page.onLoadFinished
		
			                }//Status==success
			            });
			        }else { // else if(urls.length) > 0
			            return callbackFinal();
			        }
			    };
			    return retrieve();
			};
			
		   	var arrayOfUrls = JSON.parse('[{"id":"81","link":"http:\/\/tunghoanh.vn\/do-thi-tuyet-sac-bang\/chuong-28-O5ebaab.html","tap":"","thutu":"29","story_id":"1"},{"id":"82","link":"http:\/\/tunghoanh.vn\/do-thi-tuyet-sac-bang\/chuong-29-P5ebaab.html","tap":"","thutu":"30","story_id":"1"},{"id":"83","link":"http:\/\/tunghoanh.vn\/do-thi-tuyet-sac-bang\/chuong-30-Q5ebaab.html","tap":"","thutu":"31","story_id":"1"},{"id":"84","link":"http:\/\/tunghoanh.vn\/do-thi-tuyet-sac-bang\/chuong-31-R5ebaab.html","tap":"","thutu":"32","story_id":"1"},{"id":"85","link":"http:\/\/tunghoanh.vn\/do-thi-tuyet-sac-bang\/chuong-32-S5ebaab.html","tap":"","thutu":"33","story_id":"1"},{"id":"86","link":"http:\/\/tunghoanh.vn\/do-thi-tuyet-sac-bang\/chuong-33-T5ebaab.html","tap":"","thutu":"34","story_id":"1"}]');
			//arrayOfUrls = JSON.parse(arrayOfUrls);
			RenderUrlsToFile(arrayOfUrls, (function(status, url, file) {
		
			}), function() {
		    return phantom.exit();
			});